package com.jucai.pppeiyouclient.view.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jucai.pppeiyouclient.MainActivity;
import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.BaseActivity;
import com.jucai.pppeiyouclient.bean.CityBean;
import com.jucai.pppeiyouclient.bean.FeedbackFormBean;
import com.jucai.pppeiyouclient.comm.CommonData;
import com.jucai.pppeiyouclient.comm.CommonRequestNetData;
import com.jucai.pppeiyouclient.comm.GlobalAddress;
import com.jucai.pppeiyouclient.utils.ActivityCollector;
import com.jucai.pppeiyouclient.utils.LogUtils;
import com.jucai.pppeiyouclient.utils.LoginUtils;
import com.jucai.pppeiyouclient.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.UserInfo;


public class FeedbackActivity extends BaseActivity {
    private static final String TAG = "FeedbackActivity";
    private EditText feedbackContent;
    private TextView feedbackSubmit;
    private Spinner feedbackSp;
    private PopupWindow window;
    private ArrayAdapter adapter;
    private ListView listView;
    private List<FeedbackFormBean> feedbackFormBeanList = new ArrayList<FeedbackFormBean>();
    private List<String> datas = new ArrayList<String>();
    private String feedbackStr = "";

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_feedback);
        ((TextView) findViewById(R.id.common_title_text)).setText("意见反馈");
        feedbackSp = (Spinner) findViewById(R.id.feedback_spinner);
        feedbackContent = (EditText) findViewById(R.id.feedback_content);
        feedbackSubmit = (TextView) findViewById(R.id.feedback_submit);
    }


    @Override
    protected void initData() {
    }

    @Override
    protected void loadNetData() {

        HashMap<String, String> param = new HashMap<>();
        param.put("service", "Accompany_Feedback.getFeedbackForm");
        param.put("member_id", LoginUtils.getMember_id(this));
        param.put("token", LoginUtils.getToken(this));
        LogUtils.e(TAG + "param = " + param);
            CommonRequestNetData.getJsonString(FeedbackActivity.this,true,param,new CommonRequestNetData.JsonStringCallBack(){
                @Override
                public void parseJsonString(String str) throws Exception {
                    LogUtils.e(TAG + "jsArr = " + str);
                    JSONArray jsonArray = new JSONObject(str).getJSONArray("data");
                    feedbackFormBeanList = new Gson().fromJson(jsonArray.toString(), new TypeToken<List<FeedbackFormBean>>() {
                    }.getType());
                    if(!feedbackFormBeanList.isEmpty()){
                        for(int i =0;i<feedbackFormBeanList.size();i++){
                            datas.add(feedbackFormBeanList.get(i).suggestion_cate_name);
                        }
                        adapter = new ArrayAdapter<String>(FeedbackActivity.this,android.R.layout.simple_spinner_item, datas);
                        feedbackSp.setAdapter(adapter);
                        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        feedbackStr = datas.get(0);
                        feedbackSp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                feedbackStr = datas.get(position);
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                    }
                }
            });
    }

    @Override
    protected void setListener() {
        findViewById(R.id.common_back).setOnClickListener(this);
        findViewById(R.id.common_home_back).setOnClickListener(this);
        feedbackSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.common_back:
                ActivityCollector.removeActivity(this);
                break;

            case R.id.common_home_back:
                startActivity(new Intent(FeedbackActivity.this, MainActivity.class));
                ActivityCollector.removeActivity(this);
                break;

            case R.id.feedback_submit:
                //提交...
                HashMap<String, String> param = new HashMap<>();
                param.put("service", GlobalAddress.SERVER_FEEDBACK);
                param.put("member_id", LoginUtils.getMember_id(this));
                param.put("token", LoginUtils.getToken(this));
                param.put("cate_name", feedbackStr);
                param.put("feedback", feedbackContent.getText().toString().trim());
                LogUtils.e(TAG + "param = " + param);

                CommonRequestNetData.getJsonObjectData(FeedbackActivity.this, param, true, new CommonRequestNetData.JsonObjectCallBack() {
                    @Override
                    public void parseJsonObject(JSONObject jsonObject) {
                        LogUtils.e(TAG + "jsObj = " + jsonObject.toString());
                        ToastUtils.showToast("提交成功");
                        ActivityCollector.removeActivity(FeedbackActivity.this);
                    }
                });
                break;

        }
    }
}
